package com.qf.controller;

import com.qf.pojo.User;
import com.qf.service.AdminService;
import com.qf.service.impl.AdminServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author 29125
 * @version V1.0
 * @Project java2203
 * @Package com.qf.controller
 * @Description:
 * @Date 2022/5/22 19:26
 */
@WebServlet("/admin")
public class AdminController  extends BaseServlet{
    //管理员登录
    public String login(HttpServletRequest request, HttpServletResponse response) {
        //获取前端数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");

        //非空验证
        if (username==null || password==null){
            //用户信息填写为空，不需携带数据，直接重定向到管理员登录页面
            return "redirect:"+request.getContextPath()+"/admin/login.jsp";
        }
        //调用service层校验用户名和密码
        AdminService adminService=new AdminServiceImpl();
        User user=adminService.login(username,password);

        //转发或重定向
        if (user!=null){
            //从session中获取用户名
            request.getSession().setAttribute("admin",user);
            //正确跳转到首页
            return "redirect:"+request.getContextPath()+"/admin/admin.jsp";
        }else {
            //不正确跳转到登录界面
            return "redirect:"+request.getContextPath()+"/admin/login.jsp";
        }
    }
}
